package com.bcu.wish.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.bcu.wish.config.GlobalConfig;
import com.bcu.wish.entity.Message;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.bcu.wish.entity.Userinfo;
import com.bcu.wish.mapper.UserinfoMapper;
import com.bcu.wish.service.UserinfoService;

import java.util.List;

@Service
public class UserinfoServiceImpl implements UserinfoService {

    @Resource
    private UserinfoMapper userinfoMapper;

    @Resource
    private GlobalConfig globalConfig;

    @Override
    public int deleteByPrimaryKey(String userinfoUserId) {
        return userinfoMapper.deleteByPrimaryKey(userinfoUserId);
    }

    @Override
    public int insert(Userinfo record) {
        return userinfoMapper.insert(record);
    }

    @Override
    public int insertSelective(Userinfo record) {
        return userinfoMapper.insertSelective(record);
    }

    @Override
    public Message selectByPrimaryKey(String userinfoUserId) {
        return Message.success(null).add(userinfoMapper.selectByPrimaryKey(userinfoUserId));
    }

    @Override
    public int updateByPrimaryKeySelective(Userinfo record) {
        return userinfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Userinfo record) {
        return userinfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(userinfoMapper.selectAll());
    }

    @Override
    public Message getExcel2(String fileName) {
        //删除userinfo中全部数据
        userinfoMapper.deleteAll();
        ExcelReader reader = ExcelUtil.getReader(globalConfig.filePath + fileName);
        List<Userinfo> userinfoList = reader.readAll(Userinfo.class);
        for (int i = 0; i < userinfoList.size(); i++) {
            Userinfo userinfo = userinfoList.get(i);
            String str="";
            double testscore1 =userinfo.getUserinfoTestScore();
            float usertestscore = (float) testscore1;
            double Readscore1 =userinfo.getUserinfoReadScore();
            float userReadscore = (float) Readscore1;
            double WanxingScore1 =userinfo.getUserinfoWanxingScore();
            float userWanxingScore = (float) WanxingScore1;
            double YufaScore1 =userinfo.getUserinfoYufaScore();
            float userYufaScore = (float) YufaScore1;
            double CihuiScore1 =userinfo.getUserinfoCihuiScore();
            float userCihuiScore = (float) CihuiScore1;
            for (int j=0; j < userinfoList.size(); j++){
                Userinfo nextuser = userinfoList.get(j);
                double nextusertestscore1 =nextuser.getUserinfoTestScore();
                float nextuserestscore = (float) nextusertestscore1;
                double Readscore2 =nextuser.getUserinfoReadScore();
                float nextuserReadscore = (float) Readscore2;
                double WanxingScore2 =nextuser.getUserinfoWanxingScore();
                float nextuserWanxingScore = (float) WanxingScore2;
                double YufaScore2 =nextuser.getUserinfoYufaScore();
                float nextuserYufaScore = (float) YufaScore2;
                double CihuiScore2 =nextuser.getUserinfoCihuiScore();
                float nextuserCihuiScore = (float) CihuiScore2;
                if (usertestscore==nextuserestscore
                        &&userReadscore==nextuserReadscore
                        &&userWanxingScore==nextuserWanxingScore
                        &&userYufaScore==nextuserYufaScore
                        &&userCihuiScore==nextuserCihuiScore){
                    if (userinfo.getUserinfoUserId().equals(nextuser.getUserinfoUserId())){
                        str=str;
                    }else{
                        str=str+"该学生成绩与学生姓名：("+nextuser.getUserinfoUserName()+")学号："+nextuser.getUserinfoUserId()+"各项成绩皆一致。";
                    }

                }
            }
            if (str!=""){
                userinfo.setUserinfoRemarks(str);
            }
            userinfoMapper.insertSelective(userinfo);
        }
        return Message.success(null);
    }

    @Override
    public Message getExcel1(String fileName) {
        //删除userinfo中全部数据
        userinfoMapper.deleteAll();
        ExcelReader reader = ExcelUtil.getReader(globalConfig.filePath + fileName);
        List<Userinfo> userinfoList = reader.readAll(Userinfo.class);
        for (int i = 0; i < userinfoList.size(); i++) {
            Userinfo userinfo = userinfoList.get(i);
            userinfoMapper.insertSelective(userinfo);
        }
        return Message.success(null);
    }

    @Override
    public Message getExcel(String fileName) {
        //删除userinfo中全部数据
        userinfoMapper.deleteAll();
        ExcelReader reader = ExcelUtil.getReader(globalConfig.filePath + fileName);
        List<Userinfo> userinfoList = reader.readAll(Userinfo.class);
        if (userinfoList.get(0).getUserinfoTestScore()==null){
            return getExcel1(fileName);
        }else if (userinfoList.get(0).getUserinfoTestScore()!=null){
            return getExcel2(fileName);
        }
        return null;
    }
}


